package com.qianbao.qianbaobusiness.ui.cashier;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.config.Global;
import com.baseandroid.utils.FormatUtil;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.BalanceAmount2Bean;

public class HistoryBalancreAdapter extends BaseRecycleViewAdapter<BalanceAmount2Bean> {
    private ClickListener mListener;

    public HistoryBalancreAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, BalanceAmount2Bean model) {
        int p = holder.getLayoutPosition();
        ViewHolder historyHolder = (ViewHolder) holder;
        if (getData().size() == 1) {
            historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round2);

            if (Global.getShopListInfo().merchantList.size() == 1) {
                historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow);
                historyHolder.balance_merchant_name_ly.setVisibility(View.GONE);
                historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line2);
            } else {
                historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line3);
                historyHolder.line_merchantname.setBackgroundResource(R.drawable.item_bg_line);
                historyHolder.balance_merchant_name_ly.setVisibility(View.VISIBLE);
                historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow_merchantname);
                setMerchantName(historyHolder.balance_merchant_name, model);
            }
        } else {
            if (p == 0) {
                if (Global.getShopListInfo().merchantList.size() == 1) {
                    historyHolder.balance_merchant_name_ly.setVisibility(View.GONE);
                } else {
                    historyHolder.balance_merchant_name_ly.setVisibility(View.VISIBLE);
                    setMerchantName(historyHolder.balance_merchant_name, model);
                }
                if (TextUtils.equals(getData().get(p).getSettleDate(), getData().get(p + 1).getSettleDate()) && TextUtils.equals(getData().get(p).getMerchantCode(), getData().get(p + 1).getMerchantCode())) {

                    if (Global.getShopListInfo().merchantList.size() == 1) {
                        historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round1);
                        historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line);
                    } else {
                        historyHolder.line_merchantname.setBackgroundResource(R.drawable.item_bg_line);
                        historyHolder.line_vertical.setBackgroundColor(ContextCompat.getColor(mContext, R.color.text_red));
                        historyHolder.balance_layout_d.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_white));
                    }
                    historyHolder.shadow_bg_d.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_f6f6f6));
                } else {
                    if (Global.getShopListInfo().merchantList.size() == 1) {
                        historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line2);
                        historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round2);
                        historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow);
                    } else {
                        historyHolder.line_merchantname.setBackgroundResource(R.drawable.item_bg_line);
                        historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line3);
                        historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round);
                        historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow_merchantname);
                    }

                }
            } else if (p == getData().size() - 1) {
                if (TextUtils.equals(getData().get(p).getSettleDate(), getData().get(p - 1).getSettleDate()) && TextUtils.equals(getData().get(p).getMerchantCode(), getData().get(p - 1).getMerchantCode())) {
                    historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round);
                    historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow);
                    historyHolder.balance_merchant_name_ly.setVisibility(View.GONE);
                    historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line3);
                } else {
                    if (Global.getShopListInfo().merchantList.size() == 1) {

                        historyHolder.balance_merchant_name_ly.setVisibility(View.GONE);
                    } else {
                        historyHolder.balance_merchant_name_ly.setVisibility(View.VISIBLE);
                        setMerchantName(historyHolder.balance_merchant_name, model);
                    }

                    if (Global.getShopListInfo().merchantList.size() == 1) {
                        historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round2);
                        historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line2);
                        historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow);
                    } else {
                        historyHolder.line_merchantname.setBackgroundResource(R.drawable.item_bg_line);
                        historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line3);
                        historyHolder.balance_layout_d.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_white));
                        historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow_merchantname);
                    }
                }
            } else {
                if ((TextUtils.equals(getData().get(p).getSettleDate(), getData().get(p + 1).getSettleDate()) && TextUtils.equals(getData().get(p).getMerchantCode(), getData().get(p + 1).getMerchantCode())) && (TextUtils.equals(getData().get(p).getMerchantCode(), getData().get(p - 1).getMerchantCode()) && TextUtils.equals(getData().get(p).getSettleDate(), getData().get(p - 1).getSettleDate()))) {
                    historyHolder.balance_layout_d.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_white));
                    historyHolder.line_vertical.setBackgroundColor(ContextCompat.getColor(mContext, R.color.text_red));
                    historyHolder.shadow_bg_d.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_white));
                    historyHolder.balance_merchant_name_ly.setVisibility(View.GONE);
                } else if (!(TextUtils.equals(getData().get(p).getSettleDate(), getData().get(p + 1).getSettleDate()) && TextUtils.equals(getData().get(p).getMerchantCode(), getData().get(p + 1).getMerchantCode())) && (TextUtils.equals(getData().get(p).getSettleDate(), getData().get(p - 1).getSettleDate()) && TextUtils.equals(getData().get(p).getMerchantCode(), getData().get(p - 1).getMerchantCode()))) {
                    historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round);
                    historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line3);
                    historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow);
                    historyHolder.balance_merchant_name_ly.setVisibility(View.GONE);
                } else if ((TextUtils.equals(getData().get(p).getSettleDate(), getData().get(p + 1).getSettleDate()) && TextUtils.equals(getData().get(p).getMerchantCode(), getData().get(p + 1).getMerchantCode())) && !(TextUtils.equals(getData().get(p).getSettleDate(), getData().get(p - 1).getSettleDate()) && TextUtils.equals(getData().get(p).getMerchantCode(), getData().get(p - 1).getMerchantCode()))) {

                    historyHolder.shadow_bg_d.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_f6f6f6));
                    if (Global.getShopListInfo().merchantList.size() == 1) {
                        historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line);
                        historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round1);
                        historyHolder.balance_merchant_name_ly.setVisibility(View.GONE);
                    } else {
                        historyHolder.balance_layout_d.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_white));
                        historyHolder.line_merchantname.setBackgroundResource(R.drawable.item_bg_round1);
                        historyHolder.line_vertical.setBackgroundColor(ContextCompat.getColor(mContext, R.color.text_red));
                        historyHolder.line_merchantname.setBackgroundResource(R.drawable.item_bg_line);
                        historyHolder.balance_merchant_name_ly.setVisibility(View.VISIBLE);
                        setMerchantName(historyHolder.balance_merchant_name, model);
                    }
                } else {
                    if (Global.getShopListInfo().merchantList.size() == 1) {
                        historyHolder.balance_merchant_name_ly.setVisibility(View.GONE);
                    } else {
                        historyHolder.balance_merchant_name_ly.setVisibility(View.VISIBLE);
                        setMerchantName(historyHolder.balance_merchant_name, model);
                    }

                    if (Global.getShopListInfo().merchantList.size() == 1) {
                        historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line2);
                        historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round2);
                        historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow);
                    } else {
                        historyHolder.line_vertical.setBackgroundResource(R.drawable.item_bg_line3);
                        historyHolder.line_merchantname.setBackgroundResource(R.drawable.item_bg_line);
                        historyHolder.balance_layout_d.setBackgroundResource(R.drawable.item_bg_round);
                        historyHolder.shadow_bg_d.setBackgroundResource(R.drawable.bg_shadow_merchantname);
                    }
                }
            }
        }
        String time1 = model.getChkAccountDate();
        time1 = FormatUtil.changeTime1(time1, "yyyy-MM-dd");
        if (Global.getShopListInfo().merchantList.size() == 1) {
            historyHolder.balance_date.setTextColor(ContextCompat.getColor(mContext, R.color.text_important));
        } else {
            historyHolder.balance_date.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
        }
        historyHolder.balance_date.setText(time1);
        try {
            String s = Double.parseDouble(model.getSettlementAmtTotal()) / 100 + "";
            s = FormatUtil.getMoneyFormat(s);
            if (s.equals("0.00")) {
                historyHolder.balance_right_arraw.setVisibility(View.INVISIBLE);
            } else {
                historyHolder.balance_right_arraw.setVisibility(View.VISIBLE);
                if (mListener != null) {
                    historyHolder.balance_count.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListener.itemClicked(model);
                        }
                    });
                }
            }
            //                historyHolder.balance_count.setText(mContext.getResources().getString(R.string.rmb) + s);
            historyHolder.balance_count.setText(s);
            historyHolder.balance_count.setTextColor(ContextCompat.getColor(mContext, R.color.text_red));
        } catch (Exception e) {
            historyHolder.balance_count.setTextColor(ContextCompat.getColor(mContext, R.color.c_4a4a4a));
            historyHolder.balance_count.setText(model.getSettlementAmtTotal());
            historyHolder.balance_right_arraw.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_history_balance, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout balance_layout_d, shadow_bg_d;
        TextView balance_date, balance_count;
        ImageView    balance_right_arraw;
        LinearLayout balance_merchant_name_ly;
        TextView     balance_merchant_name;
        View         line_vertical, line_merchantname;

        public ViewHolder(View rootView) {
            super(rootView);
            balance_layout_d = (RelativeLayout) rootView.findViewById(R.id.balance_layout_d);
            balance_date = (TextView) rootView.findViewById(R.id.balance_date);
            balance_count = (TextView) rootView.findViewById(R.id.balance_count);
            shadow_bg_d = (RelativeLayout) rootView.findViewById(R.id.shadow_bg_d);
            balance_right_arraw = (ImageView) rootView.findViewById(R.id.balance_right_arraw);
            balance_merchant_name_ly = rootView.findViewById(R.id.balance_merchant_name_ly);
            balance_merchant_name = rootView.findViewById(R.id.balance_merchant_name);
            line_vertical = rootView.findViewById(R.id.line_vertical);
            line_merchantname = rootView.findViewById(R.id.line_merchantname);
        }
    }

    public void setMerchantName(TextView view, BalanceAmount2Bean model) {
        for (int i = 0; i < Global.getShopListInfo().merchantList.size(); i++) {
            if (TextUtils.equals(Global.getShopListInfo().merchantList.get(i).posMerchantNo, model.getMerchantCode())) {
                view.setText(Global.getShopListInfo().merchantList.get(i).merchantName);
                break;
            }
        }
    }

    public interface ClickListener {
        void itemClicked(BalanceAmount2Bean model);
    }

    public void setItemClickListener(ClickListener listener) {
        mListener = listener;
    }
}
